var vm = new Vue({
	el:'#wrapper',
	data:{
		swiper:'',
		showType:0,
		defaultPhone:'18756197235',
		phone:[
			{tel:"18756197235"},
			{tel:"12132131123"}
		],
		address:[
			{village:'合力家园一村',build:'16幢',unit:'二单元',door:'205',road:'望江路与肥西路交口',lane:'友人巷'},
			{village:'合力家园二村',build:'17幢',unit:'四单元',door:'605',road:'望江路与肥西路交口',lane:'友人巷'}
		],
		checkAddr:'',
		addAddress:{
			village:'',
			build:'',
			unit:'',
			door:'',
			road:'',
			lane:''
		},
		demandArr:[],
		demand:{
			addr:null,
			direct:null,
			important:'是',
			homeType:null,
			decorateType:null,
			decorateStyle:null,
			prop:null,
			val:null,
			open:true,
		},
		editIndex:null,
	},
	methods:{
		//判断当前页信息类别
		getInfoType:function(){
			let type = getUrlParam('type');
			type == null ? type = 0 : type;
			$("#infoNav").find('a').eq(type).addClass('on');
			this.showType = type;
		},
		//信息类别切换
		infoNav:function(type){
			let id = getUrlParam('userId');
			location.replace('user_editInfo.html?userId='+id+'&type='+type);
		},
		//选择性别
		selSex:function(e){
			let target = e.currentTarget;
			let arr = ['男','女','保密'];
			dmSelect(arr,function(i){
				target.children[1].innerHTML = arr[i];
			});
		},
		//添加手机号
		addPhone:function(i){
			let phone = this.phone;
			if(typeof i == 'number'){
				//编辑
				dmComfirm('编辑手机号',{
					text:phone[i].tel,
					success:function(val){
						phone[i].tel = val ;
					},
				});
			}else{
				//新增
				dmComfirm('新增手机号',{
					success:function(val){
						let obj = {
							tel:val,
							def:false
						};
						phone.push(obj);
					},
				});
			};
		},
		//删除手机号
		delPhone:function(tel){
			let phone = this.phone;
			for(i in phone){
				if(phone[i].tel == tel){
					phone.splice(i,1);
					this.defaultPhone == tel ? this.defaultPhone = phone[0].tel : '' ;
				};
			};
		},
		//新增地址
		addAddr:function(i){
			var that = this;
			if(typeof i == 'number'){
				//修改
				console.log(i);
				$("#addressMask .header .title").text('编辑地址');
				$("#addressMask").attr('data-type','edit');
				$("#addressMask").attr('data-index',i);
				that.addAddress = that.address[i];
				openSubMain('#addressMask');
			}else{
				//新增
				$("#addressMask .header .title").text('新增地址');
				$("#addressMask").attr('data-type','add');
				that.addAddress = {
					village:'',
					build:'',
					unit:'',
					door:'',
					road:'',
					lane:''
				};
				openSubMain('#addressMask');
			};
		},
		//确定新增地址
		enterAddr:function(){
			var that = this;
			if(that.addAddress.village != '' && that.addAddress.build != '' && that.addAddress.unit != ''){
				
				if($("#addressMask").attr('data-type') == 'add'){
					//新增
					that.address.push(that.addAddress);
				}else{
					//编辑
					var index = $("#addressMask").attr('data-index');
					that.address[index] = that.addAddress;
				};
				closeSubMain( $("#addressMask") );
			};
		},
		
		//删除地址
		delAddr:function(index){
			this.address.length > 1 ? this.address.splice(index,1) : dmAlert('至少保留1个地址',{
				type:1,enter:'我知道了'
			});
		},
		
		
		//客户需求模块
		
		//选择产品意向
		/*selDirect:function(e,type,index){
			var that = this;
			let target = e.currentTarget;
			if(that.demandArr.length > 0){
				//多
				that.demandArr[index].direct[type] = target.checked;
			}else{
				//空
				that.demand.direct[type] = target.checked;
			};
		},*/
		//是否跟踪
		selImt:function(index){
			var that = this;
			var arr = ['是','否'];
			
			if(typeof index == 'number'){
				if(that.demandArr[index].open){
					dmSelect(arr,function(i){
				
						if(that.demandArr.length > 0){
							//多
							that.demandArr[index].important = arr[i];
						}else{
							//空
							that.demand.important = arr[i];
						};
						
					});
				};
			}else{
				dmSelect(arr,function(i){
				
				if(that.demandArr.length > 0){
					//多
					that.demandArr[index].important = arr[i];
				}else{
					//空
					that.demand.important = arr[i];
				};
				
			});
			};
		},
		//装修类型
		selDecorateType:function(index){
			var that = this;
			var arr = ['新房装修','老房改造'];
			dmSelect(arr,function(i){
				
				if(that.demandArr.length > 0){
					//多
					that.demandArr[index].decorateType = arr[i];
				}else{
					//空
					that.demand.decorateType = arr[i];
				};
				
			});
		},
		//删除当前需求
		delDemand:function(index){
			var that = this;
			that.demandArr.splice(index,1);
			that.swiper.update();
		},
		//编辑当前需求
		editDemand:function(index){
			var that = this;
			this.demandArr[index].open = true;
			that.swiper.update();
			that.editIndex = index;
			that.checkAddr='';
		},
		//保存当前需求
		saveDemand:function(index){
			var that = this;
			if(that.demandArr.length > 0){
				//多
				that.demandArr[index].open = false;
			}else{
				//空
				that.demand.open = false;
				that.demandArr.push(that.demand);
			};
			that.demand = {
				addr:null,
				direct:null,
				important:'是',
				homeType:null,
				decorateType:null,
				decorateStyle:null,
				prop:null,
				val:null,
				open:true,
			};
			that.editIndex = null;
			that.checkAddr='';
			that.swiper.update();
		},
		//新增
		increase:function(){
			var that = this;
			that.demandArr.push(that.demand);
			that.swiper.update();
		},
		
		//选择小区地址
		openAddr:function(i){
			var that = this;
			if(typeof i == 'number'){
				if(that.demandArr[i].open){
					that.demandArr[i].submitType == 0 ? openSubMain('#selAddrMask') : '';
				};
			}else{
				openSubMain('#selAddrMask');
			};
			
		},
		//确定选择的地址
		changeAddr:function(index){
			var that = this;
			closeSubMain($("#selAddrMask"));
			var item = that.address[index];
			if(that.editIndex == null){
				that.demand.addr = item.village+item.build+item.unit+item.door+item.road+item.lane;
			}else{
				that.demandArr[that.editIndex].addr = item.village+item.build+item.unit+item.door+item.road+item.lane;
			};
		},
	},
	mounted:function(){
		this.swiper = pullSwiper('#Main');
		pullSwiper('#addressMod');
		pullSwiper('#selAddrMod');
	},
	created:function(){
		this.getInfoType();
	},
	watch:{
		defaultPhone:function(val){
			console.log(val);
		},
		editIndex:function(val){
			console.log(val);
		},
	},
});